package com.disgruntledbots;

import java.util.ArrayList;

import org.andengine.engine.Engine;
import org.andengine.engine.FixedStepEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.IResolutionPolicy;
import org.andengine.engine.options.resolutionpolicy.RelativeResolutionPolicy;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.ui.activity.LayoutGameActivity;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.examplebox2dtest.R;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainMenuActivity extends LayoutGameActivity {

	Button fistLevel;
	Scene mScene;
	Engine mEngine;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*The way this method is used is almost identical to an android activity
		 * 
		 * EXCEPT THAT YOU DO NOT GET THE LAYOUT HERE
		 * 
		 * There is a separate method called getLayout() where the layout is set
		 * */
		
		Log.w("com.disgruntledbots", "OnCreate Main Menu");

		Button fistLevel = (Button) findViewById(R.id.Level1);
		fistLevel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent openFirstLevel = new Intent(MainMenuActivity.this, MultiplayerActivity.class);
                startActivity(openFirstLevel);
                MainMenuActivity.this.finish();
            }
        });
        
	}
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		
		Log.w("com.disgruntledbots", "OnCreateEngineOptions");
		
		/*This is not the android camera for accessing the device on your phone
		 * it basically defines the view that the player sees.
		 * 
		 * The parameters are the bound of the screen in pixels
		 * 
		 * Out of laziness the resolution is set to that of my tablet
		 * */	
		Camera mCamera = new Camera(0,0, 800, 480);
		
		/*EngineOptions is a set of parameters about how the engine will draw the game
		 * 
		 * public EngineOptions(final boolean pFullscreen, final ScreenOrientation pScreenOrientation, final IResolutionPolicy pResolutionPolicy, final Camera pCamera)
		 * 
		 * Resolution policy defines how the engine handles differences between camera and screen
		 * There are 4 policies FillResolutionPolicy, FixedResolutionPolicy, RatioResolutionPolicy, and RelativeResolution policy
		 * 
		 * FillResolutionPolicy fills the screen regardless, but will distort sprites in order to do so
		 * 
		 * FixedResolutionPolicy forces a resolution regardless of screensize meaning that it can smaller screens can have parts cut off
		 * 		and large screens will have black space if not configured correctly
		 * 
		 * RatioResolutionPolicy ensures the screen will have it's dimensions at a fixed ratio.
		 * 
		 * RelativeResolution allows the display area to be scaled to fit the display
		 * 
		 * */
		EngineOptions options = new EngineOptions(
				true, ScreenOrientation.LANDSCAPE_FIXED, new RelativeResolutionPolicy(1f), mCamera);
		
		Log.w("com.disgruntledbots", "Finished Engine Options");
		
		return options;
	}
	
	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions)
	{	
		
		Log.w("com.disgruntledbots", "OnCreateEngine");
		/*Fixed step engine is the one you want to use, since it insists the game run at a specified number of 
		 * steps per second as opposed to a regular Engine, there is also a LimitedFPSEngine which will slow the game if it tries
		 * to run over a specified number of frames per second. There is also a DoubleScreenSplitScreenEngine which can be used
		 * For Multiplayer
		 * 
		 * */
		Engine engine = new FixedStepEngine(pEngineOptions, 60);//60 is the number of frames per second
		Log.w("com.disgruntledbots", "Finished OnCreateEngine");
		return engine;
	}

	//Not having this was giving me trouble at one point, so now I just include it.
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		Log.w("com.disgruntledbots", "OnCreateMenuOptions");
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	//This is where textures, sounds, fonts, etc. are loaded
	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();//This must be called a the end of the method.

	}

	//Scene is what actually will be managing the objects in AndEngine
	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		Log.w("com.disgruntledbots", "Create Scene");
		mScene = new Scene();
		mScene.setBackground(new Background(256,256,256) );
		
		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		
		/*This method is generally where entities are created and added to the scene
		 * */
		pOnPopulateSceneCallback.onPopulateSceneFinished();//This must be called a the end of the method.
	}

	//This is the method where the layout is set
	//The Layout should be a relative layout
	@Override
	protected int getLayoutID() {
		Log.w("com.disgruntledbots", "Get LayoutID");
		return R.layout.activity_mainmenu;
	}

	//This gets the gamesurface that must be set in your layout
	@Override
	protected int getRenderSurfaceViewID() {
		Log.w("com.disgruntledbots", "RenderSurfaceView");
		return R.id.gameSurfaceView2;
	}

}
